package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Type;
import com.sdry.service.hpf.TypeService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: TypeController 
 * @Description:产品型号Controller
 * @Author hpf
 * @Date 2018年9月20日 下午3:59:10 
 * @version 1.0
 */
@RequestMapping(value="/typeMana")
@Controller
public class TypeController {

	@Resource
	private TypeService typeService;
	
	/**
	 * 分页查询产品型号
	 * @param page 分页条件
	 * @param type 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageType")
	public void listPageType(Page page,Type type,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (type.getTname() != null) {
			map.put("tname", type.getTname());
		} else {
			map.put("pnum", "");
			type.setTname("");
		}
		int count = typeService.countType(type);
		List<Type> typeList = typeService.listPageType(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(typeList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存产品型号
	 * @param type
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveType")
	public void saveType(Type type,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (type.getTid() == null || "".equals(type.getTid())) {
			isSuccess = typeService.insertType(type);
		} else {
			isSuccess = typeService.updateType(type);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除产品型号
	 * @param tid 产品型号ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteType")
	public void deleteType(Long tid,HttpServletResponse response) throws Exception{
		int isSuccess = typeService.deleteType(tid);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 批量删除产品型号
	 * @param idArr 产品型号ID数组
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTypeByArr")
	public void deleteTypeByArr(@RequestParam("idArr[]") String[] idArr,HttpServletResponse response) throws Exception{
		int isSuccess = typeService.deleteTypes(idArr);
		ResponseUtil.write(response, isSuccess);
	}
}
